package io.renren.modules.canteens.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.canteens.dao.BatchOrderDao;
import io.renren.modules.canteens.dto.*;
import io.renren.modules.canteens.entity.BatchOrderEntity;
import io.renren.modules.canteens.service.*;
import io.renren.modules.sys.dto.SysDeptDTO;
import io.renren.modules.sys.service.SysDeptService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 批量采购表
 *
* @author 翟文闯 619556390@QQ.com
 * @since 1.0.0 2020-01-03
 */
@Service
public class BatchOrderServiceImpl extends CrudServiceImpl<BatchOrderDao, BatchOrderEntity, BatchOrderDTO> implements BatchOrderService {
    @Autowired
    private BatchOrderDao batchOrderDao;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private BatchOrderService batchOrderService;
    @Autowired
    private BatchOrdertemService batchOrdertemService;
    @Autowired
    private BatchOrdermxService batchOrdermxService;
    @Autowired
    private FinanceService financeService;
    @Override
    public QueryWrapper<BatchOrderEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<BatchOrderEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public BatchOrderDTO getTotalMoney(Long batchOrderId) {
        BatchOrderDTO totalMoney = batchOrderDao.getTotalMoney(batchOrderId);
        return totalMoney;
    }
    @Override
    public PageData<BatchOrderDTO> getOrder(Map<String, Object> params) {
        QueryWrapper<BatchOrderEntity> wrapper = new QueryWrapper<BatchOrderEntity>();
        String status = (String)params.get("status");
        String canteenId = (String)params.get("canteenId");
        String batchOrderName =(String)params.get("batchOrderName");
        String supplierId =(String)params.get("supplierId");

        wrapper.eq(StringUtils.isNotBlank(status),"status",status);
        wrapper.eq(StringUtils.isNotBlank(canteenId),"canteen_id",canteenId);
        wrapper.eq(StringUtils.isNotBlank(batchOrderName),"batch_order_name",batchOrderName);
        wrapper.eq(StringUtils.isNotBlank(supplierId),"supplier_id",supplierId);
        wrapper.orderByDesc("create_date");
        IPage<BatchOrderEntity> page = baseDao.selectPage(
                getPage(params, null, false),
                wrapper
        );
        PageData<BatchOrderDTO> pageData = getPageData(page, BatchOrderDTO.class);
        return pageData;
    }

    @Transactional
    public void saveOrder(BatchOrderDTO dto) {
        SysDeptDTO sysDeptDTO = sysDeptService.get(dto.getCanteenId());
        dto.setBatchOrderName(sysDeptDTO.getName()+"批量采购清单");
        batchOrderService.save(dto);
        Long batchOrderId = dto.getBatchOrderId();
        List<BatchOrdertemDTO> batchOrdermx = batchOrdertemService.getBatchOrdermx();
        for (BatchOrdertemDTO ordertemDTO:batchOrdermx) {
            BatchOrdermxDTO batchOrdermxDTO = new BatchOrdermxDTO();
            BeanUtils.copyProperties(ordertemDTO,batchOrdermxDTO);
            batchOrdermxDTO.setBatchOrderId(batchOrderId);
            batchOrdermxDTO.setSupplierId(dto.getSupplierId());
            batchOrdermxService.save(batchOrdermxDTO);
            batchOrdertemService.deleteById(ordertemDTO);
        }


    }

    @Override
    public Map getOrdermx(BatchOrderDTO dto) {
        HashMap<String, Object> hashMap = new HashMap<>();
        BatchOrderDTO batchOrderDTO = batchOrderService.get(dto.getBatchOrderId());
        List<BatchOrdermxDTO> ordermx = batchOrderDao.getOrdermx(dto.getBatchOrderId());
        BatchOrdermxDTO totalMoney = batchOrdermxService.getTotalMoney(batchOrderDTO.getBatchOrderId());
        hashMap.put("batchOrderDTO",batchOrderDTO);
        hashMap.put("ordermx",ordermx);
        hashMap.put("totalMoney",totalMoney.getTotalMoney());
        return hashMap;
    }

    @Transactional
    public void updateDto(BatchOrderDTO dto) {
            if (StringUtils.isBlank(dto.getIds())) {
                batchOrderService.update(dto);
            } else {
                String[] arr = dto.getIds().split(",");
                for (String id : arr) {
                    BatchOrderDTO batchOrderDTO = batchOrderService.get(Long.parseLong(id));
                    if (null != batchOrderDTO || 0 == batchOrderDTO.getStatus()) {
                        batchOrderDTO.setStatus(1);
                        batchOrderService.update(batchOrderDTO);
                        saveFinance(Long.parseLong(id));
                    }
                }
            }

    }
    /**
     * 生成财务订单
     * @param order
     */
    void saveFinance(Long order){
        BatchOrderDTO batchOrderDTO = batchOrderService.get(order);
        BatchOrdermxDTO totalMoney = batchOrdermxService.getTotalMoney(order);
        FinanceDTO financeDTO = new FinanceDTO();
        financeDTO.setCanteenId(batchOrderDTO.getCanteenId().toString());
        financeDTO.setCreateDate(batchOrderDTO.getCreateDate());
        financeDTO.setAmountPayable(totalMoney.getTotalMoney());
        financeDTO.setSupplierId(batchOrderDTO.getSupplierId());
        financeDTO.setOrderid(batchOrderDTO.getBatchOrderId());
        financeDTO.setType("4");
        financeService.save(financeDTO);

    }
}